#include "InetAddr.hpp"
#include "Log.hpp"
#include "NetCal.hpp"
#include "Protocol.hpp"
#include "Socket.hpp"
#include "TcpServer.hpp"
#include "Daemon.hpp"
#include <memory>
#include <string>
#include <unistd.h>

void usage(std::string proc) {
    std::cerr << "the usage is " << proc << " + port!" << std::endl;
}
int main(int argc, char* argv[]) {
    if(argc != 2) {
        usage(argv[0]);
    }
    std::cout << "服务器启动了，已经是守护进程了" << std::endl;
    Daemon(1, 1);
    // Daemon(0, 0);
    EnableConsoleLogStrategy();
    std::unique_ptr<Cal> cal = std::make_unique<Cal>();
    std::unique_ptr<Protocol> protocol = std::make_unique<Protocol>([&cal](Requset& req)->Response {
        return cal->Execute(req);
    });
    std::unique_ptr<TcpServer> ts = std::make_unique<TcpServer>(std::stoi(argv[1]), [&protocol](std::shared_ptr<Socket>& sock, InetAddr& client) {
        protocol->GetRequest(sock, client);
    });
    ts->Start();
    return 0;
}